# controller

# responsible for the gui-elements to choose a raid-level-configuration
# this view is only generated, when there are three or more than three storage-services available
# otherwise it will load the regular view to select-data-page...

from Utilities import *
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from model.octopus.raid.RaidLevelConfig import *

class BuildChooseRaidLevel(webapp.RequestHandler):
    def get(self, pSelf, templateValues):
        utilities = Utilities()
        
        # menu-text
        credentialsText = utilities.getMenuTextCredentials()
        yourDataText = utilities.getMenuTextYourData()
        infoText = utilities.getMenuTextInfo()
        
        # get same basic place-holder
        title = utilities.getTitleText()
        userText = templateValues['userText']
        username = templateValues['user']
        url = templateValues['url']
        urlText = templateValues['urlText']
        hintText = templateValues['hintText']
        hintColor = templateValues['hintColor']
        
        # determine the available storage-services
        credentials = utilities.getCredentials(username)
        credentialsAmount = credentials.count()
        results = credentials.fetch(100)
        
        if credentialsAmount == 3:
            # determine if a raid-level for this user is already chosen
            raidLevel = utilities.getRaidLevel(username)
            storagesEmpty = utilities.getPropertyStoragesEmpty(username)

            # check if the storage-services are empty or not
            areEmpty = utilities.prepareToPersist(username, results)
            
            if areEmpty == False:
                if raidLevel == utilities.getRaidOneText() or raidLevel == utilities.getRaidFiveText():
                    pSelf.redirect('/selectdata')
                    
            if areEmpty == True or raidLevel == None:
                # get some specific place-holder
                raidLevelHeadline = self.getRaidLevelHeadline()
                raidLevelText = self.getRaidLevelText()
                raidLevelMaskHeadline = self.getRaidLevelMaskHeadline()
                raidLevelMask = self.buildRaidLevelMask(utilities)
            
                finalTemplateValues = {
                                       'title': title,
                                       'userText': userText,
                                       'user': username,
                                       'url': url,
                                       'urlText': urlText,
                                       'hintText': hintText,
                                       'hintColor': hintColor,
                                       'credentials': credentialsText,
                                       'yourData': yourDataText,
                                       'info': infoText,
                                       'chooseRaidLevelHeadline': raidLevelHeadline,
                                       'chooseRaidLevelText': raidLevelText,
                                       'raidLevelMaskHeadline': raidLevelMaskHeadline,
                                       'chooseRaidLevelMask': raidLevelMask
                                       }
                    
                html = template.render('view/templates/chooseraidlevel.html', finalTemplateValues)
                
                pSelf.response.out.write(html)
        
        # if there are three storage-services available, then show the choose-raid-level page
        if credentialsAmount < 3:
            pSelf.redirect('/selectdata')

    
    # contains the head-line of the page
    def getRaidLevelHeadline(self):
#        headline = _("RAID-level configuration")
        headline = "RAID-level configuration"
        return headline
    
    # contains the information-text of the page
    def getRaidLevelText(self):
#        infoText = _("You are connected to three cloud-based-storage-services. Please choose one of the available RAID-level.\n"
#                     "Note that your choice can't be undone, unless you delete all files from all storages and choose another RAID-Level.")
        infoText = "<p>You are connected to three cloud-based-storage-services. Please choose one of the available RAID-level.</p>"
        infoText = infoText +  "<p>Note, that your choice can't be undone, unless you delete all files from all storages and choose another RAID-level.</p>"
        return infoText
    
    # contains the headline of the raid-level-mask
    def getRaidLevelMaskHeadline(self):
#        maskHeadline = _("Choose a RAID-Level")
        maskHeadline = "Choose a RAID-Level"
        return maskHeadline
    
    # build the gui-elements to make a decision between the available raid-level
    def buildRaidLevelMask(self, utilities):
        raidLevel = 'RAIDLevel'
        raidOne = 'RAID-1'
        raidFive = 'RAID-5'
        
        form = ''
        form = form + '<form action="/selectdata" method="post" accept-charset="utf-8">'
        form = form + '<table border="0">'
        
        form = form + '<tr>'
        form = form + '<td align="center">'
        form = form + '<input type="radio" name="' + raidLevel + '" value="'+ raidOne +'" />'
        form = form + raidOne
        form = form + '</td>'
        form = form + '<td>'
        form = form + '<a href="/raidoneinfo" title="info" />'
#        form = form + '<img src="view/images/info.png" width="20" height="20" border="0" alt="info">'
#        form = form + '<img src="view/images/OctopusIconsScratch/02_Scratch/user_info.png" width="32" height="32" border="0" alt="info">'
        form = form + '<img src="view/images/OctopusIconsScratch/01_Scratch/bullet_info.png" width="28" height="28" border="0" alt="info">'
        form = form + '</a>'
        form = form + '</td>'
        form = form + '</tr>'
        
        form = form + '<tr>'
        form = form + '<td>'
        form = form + '</td>'
        form = form + '</tr>'
        
        form = form + '<tr>'
        form = form + '<td align="center">'
        form = form + '<input type="radio" name="' + raidLevel + '" value="' + raidFive + '" />'
        form = form + raidFive
        form = form + '</td>'
        form = form + '<td>'
        form = form + '<a href="/raidfiveinfo" title="info" />'
#        form = form + '<img src="view/images/info.png" width="20" height="20" border="0" alt="info">'
#        form = form + '<img src="view/images/OctopusIconsScratch/02_Scratch/user_info.png" width="32" height="32" border="0" alt="info">'
        form = form + '<img src="view/images/OctopusIconsScratch/01_Scratch/bullet_info.png" width="28" height="28" border="0" alt="info">'
        form = form + '</a>'
        form = form + '</td>'
        form = form + '</tr>'
        form = form + '</table>'
        
        form = form + '<table>'
        form = form + '<tr>'
        form = form + '<td>'
        form = form + '<input type="submit" value="continue" />'
        form = form + '</td>'
        form = form + '</tr>'
        form = form + '</table>'
        form = form + '</form>'
        
        return form
